package com.rpc.center.event.listener;

import com.rpc.center.entity.PublisherNode;
import com.rpc.center.event.eventtype.HeartBeatEvent;
import com.rpc.center.event.eventtype.EventType;
import com.rpc.center.manager.RPCServiceManager;
import com.rpc.common.entity.request.HeartBeatDTO;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HeartBeatListener extends EventListener<HeartBeatEvent> {
    @Autowired
    private RPCServiceManager manager;
    @Override
    public void actionListener(HeartBeatEvent event) {
        HeartBeatDTO heartBeatDTO=event.getMessage();
        Channel channel=event.getChannel();
        PublisherNode node=manager.getPublisherByChannel(channel);
        if(node==null){
            //node为null说明了此节点为掉线重连
            reOnline(channel);
            return;
        }
        node.setLastKeepLiveTime(System.currentTimeMillis());
    }

    public HeartBeatListener() {
        this.setEventType(EventType.HEART_EVENT);
    }

    private void reOnline(Channel channel){
        //todo
    }
}
